package cn.nfwx.tv.manager;

import android.content.Context;
import android.util.Log;

import cn.nfwx.tv.callback.ResponseListener;
import cn.nfwx.tv.callback.ResponseWithFailListener;
import cn.nfwx.tv.conts.Constants;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.okhttp.CustomOkHttpClient;
import cn.nfwx.tv.okhttp.UrlHelper;

/**
 * Created by dell on 2017/11/13.
 */

public class SubscriptionManager {
    private static String list_url = Constants.test_url + "/nfwx-user/v1/subscription?";


    public static void requestMySubscriptionList(Context context, int pageNo, int pageSize, String token, ResponseWithFailListener<String> callBack) {
        CustomOkHttpClient.getObjectRequestWithHeader(context, list_url + "page=" + pageNo + "&per_page=" + pageSize, "WatchRecordManager", token, callBack);

    }

    public static String requestVideoList(Context context, String cid, ResponseListener callBack) {
        String request_tag = "requestHotColumnList";
        StringBuilder builder = new StringBuilder(Constants.base_video_url + "/api?");
        builder.append("action=listVideos");
        builder.append("&cid=" + cid);
        builder.append("&details=true");
        builder.append("&sort=monhot asc");
        builder.append("&sort=created_time desc");
        builder.append("&type=video");
        builder.append("&uid=").append(Constants.NFWX_UID);
        builder.append("&referer=").append(Constants.base_video_url);
        Log.d("url:", builder.toString());
        CustomOkHttpClient.postObjectRequest(context, builder.toString(), request_tag, callBack);
        return request_tag;
    }

    public static void cancelSubscribe(Context context, String cids, String header, ResponseListener<String> callBack) {
        CustomOkHttpClient.deleteObjectRequestWithBody(context, list_url, cids, "WatchRecordManager", header, callBack);
    }

    /**
     * 获取热门订阅列表
     *
     * @param context
     * @param responseListener
     */
    public static String requestHotSubscriptionList(Context context, ResponseListener responseListener) {
        String request_tag = "requestHotSubscriptionList";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkHotSubscriptUrl();
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }

    /**
     * 获取我的订阅
     *
     * @param context
     * @param responseListener
     */
    public static String requestMySubscriptionList(Context context, ResponseListener responseListener) {
        String request_tag = "requestMySubscriptionList";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkMySubscriptUrl(AccountHelper.getInstance().getUid(context));
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }

    /**
     * 增加订阅
     *
     * @param context
     * @param subject_id
     * @param responseListener
     * @return
     */
    public static String requestAddSubscription(Context context, int subject_id, ResponseListener responseListener) {
        String request_tag = "requestAddSubscription";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkAddDelSubscriptUrl(subject_id, AccountHelper.getInstance().getUid(context), UrlHelper.IJK_ADD);
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }

    /**
     * 删除订阅
     *
     * @param context
     * @param subject_id
     * @param responseListener
     * @return
     */
    public static String requestDelSubscription(Context context, int subject_id, ResponseListener responseListener) {
        String request_tag = "requestDelSubscription";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkAddDelSubscriptUrl(subject_id, AccountHelper.getInstance().getUid(context), UrlHelper.IJK_DEL);
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }
}
